package com.chen.service.vip;

import com.chen.dao.VipMapper;
import com.chen.pojo.Vip;
import com.chen.util.SystemConstrant;
import com.chen.vo.VipVo;

import java.util.Date;
import java.util.List;

public class VipServiceImpl implements VipService {
    private VipMapper vipMapper;

    public void setVipMapper(VipMapper vipMapper) {
        this.vipMapper = vipMapper;
    }

    public List<Vip> queryAllVip() {
        return vipMapper.queryAllVip();
    }
    /*登录*/
    public Vip login(String vipNo, String vipPassword) {
        Vip vip = vipMapper.login(vipNo);
        //判断对象是否为空
        if (vip!=null){
            //比较密码是否一致
            if (vip.getVipPassword().equals(vipPassword)){
                return vip;//登录成功
            }
        }
        return null;//登录失败
    }

    public List<Vip> findEmployeeList(VipVo vipVo) {
        return vipMapper.findEmployeeList(vipVo);
    }

    public int addEmployee(Vip vip) {
        return vipMapper.addEmployee(vip);
    }

    public int updateEmployee(Vip vip) {
        return vipMapper.updateEmployee(vip);
    }

    public int deleteById(Integer vipId) {
        return vipMapper.deleteById(vipId);
    }

    public int resetPwd(Integer vipId) {
        Vip vip = new Vip();
        vip.setVipPassword(SystemConstrant.DEFAULT_VIP_PASSWORD);
        vip.setRemark(SystemConstrant.DEFAULT_VIP_PASSWORD);
        vip.setVipId(vipId);
        return vipMapper.updateEmployee(vip);
    }

    public boolean saveEmployeeRole(String roleIds, Integer empId) {
        try {
            //先删除员工角色关系表的数据
            vipMapper.deleteEmployeeAndRole(empId);
            //再保存员工角色关系
            String [] idStr = roleIds.split(",");
            for (int i = 0; i < idStr.length; i++) {
                vipMapper.addEmployeeRole(idStr[i],empId);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public int addVip(Vip vip) {
        return vipMapper.addVip(vip);
    }

    public Vip findVipByNo(String vipNo) {
        return vipMapper.findVipByNo(vipNo);
    }

    public Vip loginFront(String vipNo, String vipPassword) {
        Vip vip = vipMapper.findVipByNo(vipNo);
        if (vip != null){
            //比较密码是否一致
            if (vip.getVipPassword().equals(vipPassword)){
                return vip;
            }
        }
        return null;
    }

    public List<Vip> findVipList(VipVo vipVo) {
        return vipMapper.findVipList(vipVo);
    }

    public int addVipHo(Vip vip) {
        vip.setVipDeptId(0);
        vip.setVipTime(new Date());
        return vipMapper.addVipHo(vip);
    }

    public int updateVip(Vip vip) {
        vip.setVipDeptId(0);
        return vipMapper.updateVip(vip);
    }
}
